[#import "/framework/JGP.htm"  as JGP]
<!--
项目   jgp-cloud-parent
作者   LOUFEI [Gavin]
时间   2019-11-03
-->
[@JGP.head "角色管理"/]
[@JGP.body]
    <jgp-div _col="['300','*']" _cunit="px" aux="true">
        <jgp-panel slot="1-1" _row="['40','*']" _runit="px">
            <jgp-tree2 _id="tree" _url="/security/role-api-ctl/query-tree-lazy"
                       _extend="true"
                       _onclick="doClickNodeFn"
                       _tools="[{'icon':'cubes','tip':'接口权限','fn':'editRes'},{'icon':'list','tip':'菜单权限','fn':'editRoleMenu'}]"
                       _check="true" _root="角色" _single="true"></jgp-tree2>
        </jgp-panel>
        <jgp-div slot="1-2" _row="['30','80','*']" _runit="px">
            <div slot="1-1">
                <jgp-btn _fn="createRole">新建角色</jgp-btn>
                <jgp-btn _fn="deleteInBatch" _color="caution">批量删除</jgp-btn>
                <jgp-btn _fn="refreshCache">刷新缓存</jgp-btn>
            </div>
            <jgp-query slot="2-1" _id="roleGridQuery" _action="/security/role-api-ctl/query" _target="roleGrid">
                <jgp-form-group _title="搜索角色">
                    <jgp-text _col="12" _name="name" _placeholder="请输入角色名"></jgp-text>
                </jgp-form-group>
            </jgp-query>
            <jgp-grid slot="3-1" _id="roleGrid" _tools_name="true"
                      _record_tools="[{'tip':'编辑','fn':'editRole','icon':'edit'},{'tip':'删除','fn':'deleteOne','icon':'trash-o'}]"></jgp-grid>
        </jgp-div>

    </jgp-div>
[/@JGP.body]

[@JGP.foot]
    <script>
        function createRole() {
            var selected = JGP.tree2('tree').getSelected();
            var parentId = -1;
            if (selected.length !== 0) {
                parentId = selected[0].id
            }

            var dialog = {
                'title': '添加角色',
                'width': '500px',
                'height': '270px',
                'url': '/security/role-ctl/edit-role-item?parentId=' + parentId,
                'callback': function (result) {
                    if(result){
                        location.reload();
                    }

                }
            };
            JGP.$dialog(dialog);
        }

        function editRole(args) {
            var dialog = {
                'title': '修改角色:' + args.back.record.name,
                'width': '500px',
                'height': '270px',
                'url': '/security/role-ctl/edit-role-item?id=' + args.back.record.id,
                'callback': function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(dialog);
        }

        function doClickNodeFn(args) {
            JGP.query('roleGridQuery').field('parentId', args.back.key)
        }


        function deleteInBatch() {
            var selected = JGP.grid('roleGrid').getSelected();
            if (selected.length === 0) return;
            JGP.$confirm("是否确定删除？", function () {
                var ids = [];
                for (var i = 0; i < selected.length; i++) {
                    ids.push(selected[i].id);
                }
                JGP.$postJson("/security/role-api-ctl/remove-role", ids, function (result) {
                    if (result.flag) {
                        JGP.$alert("删除成功");
                        JGP.query('roleGridQuery').search();
                    }
                })
            });
        }

        function deleteOne(args) {
            JGP.$confirm("是否确定删除？", function () {
                var ids = [args.back.record.id];

                JGP.$postJson("/security/role-api-ctl/remove-role", ids, function (result) {
                    if (result.flag) {
                        JGP.$alert("删除成功");
                        JGP.query('roleGridQuery').search();
                    }
                })
            });
        }

        function refreshCache() {
            JGP.$post("/security/role-api-ctl/refresh-cache", {}, function (result) {
                if (result.flag) {
                    JGP.$alert("刷新成功成功");
                }
            })
        }

        function editRoleMenu(args) {
            var id = args.back.node.bean.id;
            var dialog = {
                'title': '修改角色:(' + args.back.node.bean.name+')的菜单权限',
                'width': '500px',
                'height': '600px',
                'url': '/security/role-menu-ctl/edit-role-menu?roleId=' + id,
                'callback': function (result) {
                    if (result) {
                        location.reload();
                    }
                }
            };
            JGP.$dialog(dialog);

        }
    </script>
[/@JGP.foot]